<?php
/**
 * 地址精度 ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/customer/custDeliver.class.php');

class ajax_addressPrecision{

    public function execute() {

        if(isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])){

            switch($_REQUEST['operFlg']){

                case 1: //查询地址列表
                    $this->addressList();
                    break;

                case 100:
                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('edit'))) {

                        returnJsonData('参数错误');

                    }

                    if ($_REQUEST['act'] == 'edit') {

                        $this->edit();//修改收货地址

                    }

                    break;

                default:
                    returnJsonData('参数错误');
                    break;
            }
        } else {
            returnJsonData('参数错误');
        }
    }

    //查询地址列表信息
    private function addressList() {

        global $G_PGLOB;

        //实例地址类
        $custDeliver = new custDeliver();

        //查询地址信息
        $arg_array = $_REQUEST;
        $arg_array['extend'] = ' AND precise > 1';
        $address_array = $custDeliver->searchCustDeliver( $arg_array );

        //组织返回的数据
        $result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
        );

        if ($address_array) {

            $edit_permission = $G_PGLOB->has_permission(23);

            foreach ($address_array as &$address) {

                $address_detail = $address['provinceName'] . $address['cityName'] . $address['countyName'] . $address['address'];

                if ($edit_permission) {
                    $result_array['rows'][] = array(
                        'id' => $address['deliverID'],
                        'cell' => array(
                            '<a href="javascript: void(0);" onclick=address_precision.edit(' . $address['deliverID'] . ',"' . $address['longitude'] . '","' . $address['latitude'] . '","' . $address_detail . '")>修改</a>',
                            $address_detail,
                            $address['longitude'],
                            $address['latitude'],
                            $address['precise']
                        )
                    );
                } else {
                    $result_array['rows'][] = array(
                        'id' => $address['deliverID'],
                        'cell' => array(
                            $address_detail,
                            $address['longitude'],
                            $address['latitude'],
                            $address['precise']
                        )
                    );
                }

            }

        }

        echo json_encode($result_array);

    }


    /**
     * 修改收货地址
     */
    private function edit() {

        //实例地址类
        $custDeliver = new custDeliver();

        //修改收货地址
        $result_array = $custDeliver->editAddress( $_REQUEST );

        returnJsonData($result_array['message'], $result_array['code']);

    }

}